const { screen, ipcMain, Menu, MenuItem } = require("electron");

function resizeWindow(mainWindow) {

    /**
     * 窗口缩小事件
     */
    ipcMain.on('window-restore-size', () => {
        let cursor = screen.getCursorScreenPoint();
        let curScreen = screen.getDisplayNearestPoint({x: cursor.x, y: cursor.y});
        let { width, height } = curScreen.workAreaSize;
        let scaleFactor = curScreen.scaleFactor;
        console.log("window-restore-size width --> " + width + " height --> " + height)
        console.log("window-restore-size scaleFactor -->" + scaleFactor)
        if(width > 1439 && height > 899){
            width = 1440;
            height = 900;
        } else {
            width = 1024;
            height = 640;
        }

        mainWindow.setBounds({
            width: width,
            height: height,
            x: curScreen.bounds.x,
            y: curScreen.bounds.y
        });
        mainWindow.center();
        mainWindow.setMovable(true);
    });

    /**
     * 全屏事件
     */
    ipcMain.on("window-full-screen", () => {
		// mainWindow.setBounds({
		//     width: 1366,
		//     height: 720,
		//     x: 0,
		//     y: 0
		// });
		// mainWindow.center();
        mainWindow.maximize();
        mainWindow.setMovable(false);
    });

    /**
     * 关闭事件
     */
    ipcMain.on('window-close', () => {
        mainWindow.close();
    });

    /**
     * 窗口最小化
     */
    ipcMain.on('window-minimize', () => {
        mainWindow.minimize();
    })

    // /**
    //  * 获取当前的屏幕尺寸
    //  */
    // ipcMain.on('get-window-size', () => {
    //     console.log("start")
    //     let mainSize = mainWindow.getContentSize();
    //     console.log(mainSize);
    // })

    // 接收标识为synchronous-message的消息，然后返回'pong'
    ipcMain.on('async-get-window-size', function (event) {
        let mainSize = mainWindow.getContentSize();
        event.sender.send('asynchronous-reply', JSON.stringify(mainSize));
    })

    /**
     * 返回软件首页
     */
    ipcMain.on('show-main-window', function () {
        if(mainWindow){
            mainWindow.show();
        }
    });

    ipcMain.on('sigShowRightClickMenu', () => {
        // 生成菜单
        const menu = new Menu();
        menu.append(new MenuItem({label:'粘贴', role: 'paste' }));
        menu.popup(mainWindow);
    });
}
module.exports = resizeWindow;
